<template>
  <el-dialog :title="!dataForm.id ? '价格组新增' : '价格组修改'" :close-on-click-modal="false" :visible.sync="visible" fullscreen center>
    <el-button class="miui-dialog-close-btn" @click="visible = false">关闭页面</el-button>
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="80px">
      <el-form-item label="名称" prop="groupName">
        <el-input v-model="dataForm.groupName"></el-input>
      </el-form-item>
      <el-form-item label="关联部门" prop="detailNameList">
        <el-popover placement="bottom" width="500" style="height: 500px;" trigger="focus">
          <el-tree :data="deptTree" ref="tree" show-checkbox node-key="id" default-expand-all :default-checked-keys="deptList" :props="defaultProps" @check-change="handleCheckChange"></el-tree>
          <el-input type="textarea" slot="reference" v-model="detailNameList"></el-input>
        </el-popover>
      </el-form-item>
      <el-form-item label="备注" prop="memo">
        <el-input v-model="dataForm.memo"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="dataFormSubmit" :loading="ischeck" v-prevent-re-click>保 存</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import {
    priceGroupGetObj,
    priceGroupAddObj,
    priceGroupPutObj
  } from '@/api/eims/price-group'
  import { fetchTree } from '@/api/admin/dept'

  export default {
    data() {
      return {
        ischeck: false,
        visible: false,
        dataForm: {
          id: 0,
          groupName: '',
          memo: '',
          status: 1,
          detailList: []
        },
        detailNameList: '',
        dataRule: {
          groupName: [{
            required: true,
            message: '请填写价格组名称',
            trigger: 'blur'
          }],
          status: [{
            required: true,
            message: '请选择价格组状态',
            trigger: 'blur'
          }],
          detailList: [{
            required: true,
            message: '请选择关联部门',
            trigger: 'blur'
          }]
        },
        deptList: [],
        deptTree: [],
        defaultProps: {
          children: 'children',
          label: 'name'
        }
      }
    },
    methods: {
      init(id) {
        this.dataForm.id = id || 0
        this.deptList = []
        this.detailNameList = ''
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            priceGroupGetObj(this.dataForm.id).then(response => {
              if(response.data.code == '0'){
                this.dataForm = response.data.data
                let name = []
                this.dataForm.detailList.forEach(item => {
                  this.deptList.push(item.deptId)
                  name.push(item.deptName)
                })
                this.detailNameList = name.toString()
              }else{
                this.$message.error(response.data.msg)
              }
            }).then(() => {
              this.getDeptList()
            })
          } else {
            this.getDeptList()
          }
        })
      },
      // 表单提交
      dataFormSubmit() {
        this.ischeck = true
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if (this.dataForm.id) {
              priceGroupPutObj(this.dataForm).then(data => {
                this.$message.success('修改成功')
                this.visible = false
                this.$emit('refreshDataList')
                setTimeout(() => {
                  this.ischeck = false
                },1000)
              }).catch(() => {
                this.ischeck = false
              })
            } else {
              priceGroupAddObj(this.dataForm).then(data => {
                this.$message.success('添加成功')
                this.visible = false
                this.$emit('refreshDataList')
                setTimeout(() => {
                  this.ischeck = false
                },1000)
              }).catch(() => {
                this.ischeck = false
              })
            }
          }else{
            this.ischeck = false
          }
        })
      },
      // 获取部门列表
      getDeptList() {
        this.deptTree = []
        fetchTree().then(result => {
          let resultData = result.data.data
          this.deptTree = resultData
        })
      },
      // 部门树change事件
      handleCheckChange(data, checked, indeterminate) {
        let treeData = this.$refs.tree.getCheckedNodes()
        let list = []
        let name = []
        treeData.forEach(item => {
          list.push({
            deptId: item.id,
            deptName: item.name
          })
          name.push(item.name)
        })
        this.dataForm.detailList = list
        this.detailNameList = name.toString()
      }
    }
  }
</script>

<style>
  .el-popover {
    height: 300px;
    overflow-y: auto;
  }
</style>
